package com.fd.controller;


import com.fd.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.fd.service.UserService;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value="/user",produces = "text/json;charset=UTF-8")
public class IndexController {
    @Autowired
    private UserService userService;

    //登录并查询显示所有
    @RequestMapping("/login")
    public String login(String username,String password,Model model){
        System.out.println("username = " + username + ", password = " + password);
        if ("hxc".equals(username) && "123456".equals(password)) {
            List<User> users = userService.getUserAll();
            model.addAttribute("list", users);
            System.out.println("users = " + users);
            return "alluser";
        } else {
            return "shibai";
        }
    }

    //跳转到登录页面
    @RequestMapping("/index")
    public String toIndex(){
            return "login";
    }

    //查询单个用户
    @RequestMapping("/selectUser")
    public String selectUser(Integer userId,Model model){
        User user = userService.getUser(userId);
        List<User> users = new ArrayList<>();
        users.add(user);

        if(users.size() == 0){
            users = userService.getUserAll();
            model.addAttribute("error","未找到");
        }
        model.addAttribute("list",users);
        System.out.println("--------------------------------users = " + users);
        System.out.println(users);
        return "alluser";
    }

    //跳转增加用户页面
    @RequestMapping("/toaddUser")
    public String toAddPaper(){
        return "addUser";
    }

    //增加用户
     @RequestMapping("/addUser")
     public String addUser(User user,Model model) {
         userService.insertUser(user);
         List<User> users = userService.getUserAll();
         model.addAttribute("list", users);
         return "alluser";
     }

    //删除用户
    @RequestMapping("/deleteUser/{id}")
    public String deleteBook(@PathVariable int id,Model model){
        userService.deleteUser(id);
        List<User> users = userService.getUserAll();
        model.addAttribute("list", users);
        return "alluser";
    }

    /*@RequestMapping("/updateBook/{id}")
    public String updateBook(@PathVariable int id,Books books){
        books.setBookID(id);

        Map<String,Object> map = new HashMap<>();
        map.put("name",books.getBookName());
        map.put("counts",books.getBookCounts());
        map.put("detail",books.getDetail());
        map.put("id",books.getBookID());
        bookService.updateBooks(map);
        System.out.println("updateBook>="+books);
        return "redirect:/book/allBook";
    }*/

    //跳转修改用户页面
    @RequestMapping("/toUpdate/{id}")
    public String toUpdate(@PathVariable int id, Model model) {
        User user = userService.getUser(id);
        model.addAttribute("user",user);
        return "updateuser";
    }


    //修改用户
    @RequestMapping("/updateUser")
    public String updateUser(User user,Model model){
        System.out.println("user = " + user);
        Integer count = userService.updateUser(user);
        System.out.println("count = " + count);
        List<User> users = userService.getUserAll();
        model.addAttribute("list", users);
        System.out.println("user>="+user);
        return "alluser";
    }

}